home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
STUTTGART
/
UNIXTOOL
/
GNU
/
GNUDBM
/
GNUDBM~
/
<tarZ$use>
/
files
/
GNUDbm
/
C
/
Gdbmreorg
< prev
next >
Wrap
Text File
|
1990-03-01
|
4KB
|
163 lines
/* gdbmreorg.c - Reorganize the database file. */
/* GNU DBM - DataBase Manager (database subroutines) by Philip A. Nelson
Copyright (C) 1989 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
You may contact the author by:
e-mail: phil@wwu.edu
us-mail: Philip A. Nelson
Computer Science Department
Western Washington University
Bellingham, WA 98226
phone: (206) 676-3035
************************************************************************/
#include "gdbm.h"
#include "extern.h"
#include "gdbmerrno.h"
#include "gdbmutils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Reorganize the database. This requires creating a new file and inserting
all the elements in the old file DBF into the new file. The new file
is then renamed to the same name as the old file and DBF is updated to
contain all the correct information about the new file. */
int gdbm_reorganize(dbm_file_info * dbf)
{
dbm_file_info *new_dbf;
char *old_name;
char *new_name;
int len;
datum key, nextkey, data;
/* Readers can not reorganize! */
if (dbf->read_write != DBM_WRITER)
{
gdbm_errno = READER_CANT_REORGANIZE;
return -1;
}
/* Construct new name for temporary file. */
len = strlen(dbf->name);
old_name = (char *) malloc(len + 1);
new_name = (char *) malloc(len + 1);
if (old_name == NULL || new_name == NULL)
{
if (old_name != NULL)
free(old_name);
gdbm_errno = MALLOC_ERROR;
return -1;
}
strcpy(old_name, dbf->name);
strcpy(new_name, dbf->name);
new_name[len - 1] = '!';
/* Touch new file and open with gdbm. */
fclose(fopen(new_name, "w"));
new_dbf = gdbm_open(new_name, dbf->header.block_size, DBM_WRITER,
dbf->fatal_err);
if (new_dbf == NULL)
{
gdbm_errno = REORGANIZE_FAILED;
free(old_name);
free(new_name);
return -1;
}
if (dbf->header.update_state == -1)
gdbm_recovery(new_dbf, FALSE);
/* For each item in the old database, add an entry in the new. */
key = gdbm_firstkey(dbf);
while (key.dptr != NULL)
{
data = gdbm_fetch(dbf, key);
if (data.dptr != NULL)
{
/* Add the data to the new file. */
gdbm_store(new_dbf, key, data);
}
else
{
/* ERROR! Abort and don't finish reorganize. */
gdbm_close(new_dbf);
gdbm_errno = REORGANIZE_FAILED;
remove(new_name);
free(old_name);
free(new_name);
return -1;
}
nextkey = gdbm_nextkey(dbf, key);
free(key.dptr);
key = nextkey;
}
/* Remove the old file... we are now comitted */
fclose(dbf->desc);
remove(old_name);
/* Move the new file to old name. */
gdbm_close(new_dbf);
if (rename(new_name, dbf->name) != 0)
{
gdbm_errno = REORGANIZE_FAILED;
free(old_name);
free(new_name);
return -1;
}
/* Fix up DBF to have the correct information for the new file. */
new_dbf = gdbm_open(dbf->name, dbf->header.block_size, DBM_WRITER,
dbf->fatal_err);
if (new_dbf == NULL)
{
gdbm_errno = REORGANIZE_FAILED;
free(old_name);
free(new_name);
return -1;
}
if (dbf->dir != NULL)
free(dbf->dir);
if (dbf->bucket != NULL)
free(dbf->bucket);
if (dbf->avail != NULL)
free(dbf->avail);
dbf->desc = new_dbf->desc;
dbf->header = new_dbf->header;
dbf->dir = new_dbf->dir;
dbf->bucket = new_dbf->bucket;
dbf->bucket_adr = new_dbf->bucket_adr;
dbf->bucket_dir = new_dbf->bucket_dir;
dbf->avail = new_dbf->avail;
dbf->avail_adr = new_dbf->avail_adr;
free(new_dbf->name);
free(new_dbf);
free(old_name);
free(new_name);
return 0;
}